﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVGuide.DataAccess.Common;
using System.Text.RegularExpressions;
using TVGuide.Common;

namespace TVGuide.Parser
{
    class Techno24Parser : IChannelParser
    {
        public List<Program> ParseSpecificDate(DateTime date)
        {
            List<Program> tvProgramList = new List<Program>();
            string URL = "http://www.24techno.ru/programma.php?data=" + date.Year + "-" + date.Month + "-" + date.Day;
            Encoding enc = Encoding.GetEncoding(1251); //Cyrillic (Windows)
            string HTMLString = HTTPClient.GetSiteContent(URL, enc, "24Techno");
            string timeWithHTMLPattern = "<p align='right'>.*?</td>";
            Regex rgx = new Regex(timeWithHTMLPattern, RegexOptions.IgnoreCase);
            MatchCollection timesWithHTML = rgx.Matches(HTMLString);
            string progTitlePattern = "<b><a href=.*?>.*?</a>.*?</b><br>";
            Regex progTitleRgx = new Regex(progTitlePattern, RegexOptions.IgnoreCase);
            MatchCollection titlesWithHTML = progTitleRgx.Matches(HTMLString);
            if (timesWithHTML.Count!=titlesWithHTML.Count)
            {
                return tvProgramList;   
            }
            int hour;
            int minute;
            for (int i = 0; i < timesWithHTML.Count; i++)
            {
                Program tvProgram = new Program();
                Utils.seperateTimeProgramName(timesWithHTML[i].Value, out hour, out minute);
                tvProgram.Name = Utils.removeHTMLTags(titlesWithHTML[i].Value);

                if (hour == 23)
                {
                    var origDate = new DateTime(
                        date.Year,
                        date.Month,
                        date.AddDays(1).Day,
                        0,
                        minute,
                        0
                        );
                    tvProgram.Date = origDate;
                    tvProgram.ChannelId = (int)Enums.OtherChannels.Techno24;
                    tvProgramList.Add(tvProgram);
                }
                else
                {
                    tvProgram.Date = new DateTime(
                    date.Year,
                    date.Month,
                    date.Day,
                    hour+1,
                    minute,
                    0
                    );
                    tvProgram.ChannelId = (int)Enums.OtherChannels.Techno24;
                    tvProgramList.Add(tvProgram);
                }
  
            }

            return tvProgramList;
        }


        public string ChannelName()
        {
            return "24Techno";
        }

        public List<Program> ParseProgram()
        {
            return ParseSpecificDate(DateTime.Today);
        }

        public List<Program> ParseProgram(DateTime date)
        {
            return ParseSpecificDate(date);
        }


        public List<Program> ParseProgramForWeek()
        {
             List<Program> programList = new List<Program>();
            for (int i = 0; i < 7; i++)
            {
               programList.AddRange(ParseProgram((DateTime)DateTime.Today.AddDays(i)));
            }
            return programList;
        }
    }
}
